﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xwang.Task.Model;

namespace Xwang.Task.Bll
{
    public partial class TaskJoinUserService : BaseService<TaskJoinUser>, IBll.ITaskJoinUserService
    {
        public int[] GetTaskCountByGroup(int userId)
        {
            var tasks = CurrentDal.LoadEntities(u => u.UserId == userId);
            //已完成
            var c1 = tasks.Count(u => u.Status == 3);
            //未完成
            var c2 = tasks.Count(u => u.Status == 1);
            //已过期
            var c3 = tasks.Count(u =>
                u.Status == 1 &&
                (u.Task.TaskEndTime != null &&
                 DateTime.Compare(DateTime.Now.AddDays(1), DateTime.Parse(u.Task.TaskEndTime.ToString())) > 0)
            );

            //待接收
            var c4 = tasks.Count(u => u.Status == 0 && u.UserId == userId && u.Task.Biller != userId);

            return new[] { c1, c2, c3 ,c4};
        }

        public List<TaskJoinUser> GetUncompleted(int userId)
        {
            var tasks = CurrentDal.LoadEntities(u => u.UserId == userId);
            return tasks.Where(u => u.Status == 1).ToList();
        }

        public List<TaskJoinUser> GetExpired(int userId)
        {
            var tasks = CurrentDal.LoadEntities(u => u.UserId == userId);
            return tasks.Where(u => u.Status == 1 &&
                (u.Task.TaskEndTime != null &&
                 DateTime.Compare(DateTime.Now.AddDays(1), DateTime.Parse(u.Task.TaskEndTime.ToString())) > 0)).ToList();
        }

        public List<TaskJoinUser> GetCompleted(int userId)
        {
            var tasks = CurrentDal.LoadEntities(u => u.UserId == userId);
            return tasks.Where(u => u.Status == 3).ToList();
        }

        /// <summary>
        /// 待接收任务
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<TaskJoinUser> GetUnRecieved(int userId)
        {
            var tasks = CurrentDal.LoadEntities(u => u.UserId == userId);
            return tasks.Where(u => u.Status == 0 && u.UserId == userId && u.Task.Biller != userId).ToList();
        }
    }
}
